﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.ProjectContract;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;
using Bricks.Site.Models.ProjectContract;

namespace ChiDao.Business.Service.Impl
{
    /// <summary>
    /// 完工项目概况
    /// </summary>
    [Export(typeof(IFinishedProjectInfoService))]
    internal class FinishedProjectInfoService : IFinishedProjectInfoService
    {
        [Import]
        protected IFinishedProjectInfoRepository FinishedProjectInfoRepository { get; set; }

        public IQueryable<FinishedProjectInfo> FinishedProjectInfoes
        {
            get { return FinishedProjectInfoRepository.Entities; }
        }


        public OperationResult Add(FinishedProjectInfoView model)
        {
            FinishedProjectInfo record = model.CopyTo<FinishedProjectInfo>();
            FinishedProjectInfoRepository.Insert(record);
            return new OperationResult(OperationResultType.Success, "新增成功", record);
        }

        public OperationResult Modify(FinishedProjectInfoView model)
        {
            PublicHelper.CheckArgument(model, "model");
            FinishedProjectInfo record = FinishedProjectInfoes.FirstOrDefault(m => m.ID == model.ID);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            record = model.CopyTo<FinishedProjectInfo>();
            if (FinishedProjectInfoRepository.Update(record) >= 0)
                return new OperationResult(OperationResultType.Success, "修改成功", record);
            return new OperationResult(OperationResultType.Error, "保存失败");
        }

        public OperationResult Delete(Guid id)
        {
            FinishedProjectInfo record = FinishedProjectInfoes.FirstOrDefault(m => m.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (FinishedProjectInfoRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }
    }
}